package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {
    private final SetmealMapper setmealMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final DishMapper dishMapper;

    @Autowired
    public SetmealServiceImpl(SetmealMapper setmealMapper, SetmealDishMapper setmealDishMapper, DishMapper dishMapper) {
        this.setmealMapper = setmealMapper;
        this.setmealDishMapper = setmealDishMapper;
        this.dishMapper = dishMapper;
    }


    /**
     * 根据id查询套餐
     *
     * @param id 套餐id
     * @return 套餐详细信息
     */
    @Override
    public SetmealVO SetmealDetail(Long id) {
        Setmeal setmeal = setmealMapper.findById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishMapper.findBySetmealId(setmeal.getId()));
        return setmealVO;
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO 套餐实体
     */
    @Override
    @Transactional //事务注解
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(StatusConstant.ENABLE);
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 分页查询套餐列表
     *
     * @param queryDTO 套餐查询dto
     * @return 分页后的套餐列表
     */
    @Override
    public PageResult<SetmealVO> pageSetmealList(SetmealPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        try (Page<SetmealVO> page = setmealMapper.list(queryDTO)) {
            return new PageResult<>(page.getTotal(), page.getResult());
        }
    }

    /**
     * 套餐起售、停售
     *
     * @param status 1起售 0停售
     * @param id     套餐id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (Objects.equals(status, StatusConstant.ENABLE)) {
            List<Dish> dishList = dishMapper.getBySetmealIdAndDisabledStatus(id, StatusConstant.DISABLE);
            if (dishList != null && !dishList.isEmpty()) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO 修改套餐dto
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //先删后插入
        setmealDishMapper.delete(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 批量删除套餐
     *
     * @param ids id列表
     */
    @Override
    @Transactional
    public void deleteBath(List<Long> ids) {
        setmealMapper.deletes(ids);
        setmealDishMapper.deletes(ids);
    }

    /**
     * c端根据分类id查询套餐
     *
     * @param setmeal 套餐实体
     * @return 套餐列表
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.Setmeallist(setmeal);
    }

    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param id 套餐id
     * @return 套餐item列表
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
